<?php

declare(strict_types=1);

namespace App\Admin\Mapper\Setting;

use App\Admin\Model\Setting\SettingCrontab;
use Illuminate\Database\Eloquent\Builder;
use Newbie\Abstracts\AbstractMapper;


class SettingCrontabMapper extends AbstractMapper {
    /**
     * @var SettingCrontab
     */
    public $model;

    public function assignModel () {
        $this->model = SettingCrontab::class;
    }

    /**
     * @param array $ids
     * @return bool
     * @throws \Exception
     */

    public function delete (array $ids): bool {
        foreach ($ids as $id) {
            $model = $this->model::find($id);
            if ($model) {
                $model->logs()->delete();
                $model->delete();
            }
        }
        return true;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch (Builder $query, array $params): Builder {
        if (isset($params['name'])) {
            $query->where('name', 'like', '%' . $params['name'] . '%');
        }
        if (isset($params['status'])) {
            $query->where('status', $params['status']);
        }
        if (isset($params['type'])) {
            $query->where('type', $params['type']);
        }
        if (isset($params['minDate']) && isset($params['maxDate'])) {
            $query->whereBetween(
                'created_at',
                [$params['minDate'] . ' 00:00:00', $params['maxDate'] . ' 23:59:59']
            );
        }
        return $query;
    }
}